import { _decorator, Component, Label, Node, UITransform } from 'cc';
import { Ally, AllyOperateType } from '../entity/Ally';
import { UIManager } from '../common/UIManager';
import AllyApi from '../api/AllyApi';
import { UserInfo } from '../common/UserInfo';
import { Utils } from '../common/Utils';
const { ccclass, property } = _decorator;

@ccclass('AllyListItem')
export class AllyListItem extends Component {
    @property(Label)
    allyName: Label
    @property(Label)
    leader: Label
    @property(Label)
    member: Label
    @property(Node)
    join: Node

    private ally: Ally

    start() {
        this.join.on(Node.EventType.TOUCH_END, this.onJoin, this)
    }


    setData(ally: Ally) {
        this.ally = ally
        this.allyName.string = ally.AllyName + '的联盟'
        this.leader.string = ally.AllyName
        this.member.string = ally.PlayerNum + ''

        if(ally.PlayerNum >= 100){
            this.join.active = false
        }

        Utils.handleEllipsis(this.allyName.node)
        Utils.handleEllipsis(this.leader.node)

        this.scheduleOnce(() => {
            let width = this.allyName.node.getComponent(UITransform).width
            console.log(width)
        },1)
       
    }

    async onJoin() {

        try {
            await UIManager.instance.modal('提示', `您确定申请加入${this.ally.AllyName}的联盟吗？`,)

            await UIManager.instance.showLoading()
            let res = await AllyApi.Execute({
                op: AllyOperateType.Apply,
                aid: this.ally.AllyId,
                uid: UserInfo.instance.villageData.uid
            })
            UIManager.instance.closeLoading()

            UIManager.instance.modal('提示', `已经提交了加入申请`,false)
        } catch (error) {

        }


    }
}


